import {describe} from 'mocha'
import {IGitSourceParams} from '../src/git-source-params'
import Sinon from 'sinon'
import * as fs from 'fs'
import * as ossUtil from '../src/oss-api-helper'
import * as executeStep from '../src/execute-step'
import proxyquire from 'proxyquire'
import { assert, expect } from 'chai';

describe('', () => {
    beforeEach(function () {
        Sinon.stub(fs, 'existsSync').returns(true)
    })
})

describe('测试Flow源下载', () => {
    it('get方法被调用', async () => {
        let input = {} as IGitSourceParams
        input.projectDir = 'any'
        let str = JSON.parse(
            '{"bucketName":"execution-component-rdc-test","objectKey":"aone2/110/1678080025072/Artifacts_1410015.tgz","objectKeyPrefix":"aone2/110/1678080025072","packageLabel":"Artifacts_1410015"}'
        )
        input.packages = [str]
        let get = Sinon.stub(ossUtil, 'get').resolves()
        await executeStep.handleFlowSource(input)
        Sinon.assert.calledOnce(get)
        get.restore()
    })
})

describe('测试Jenkins源下载', () => {
    it('第一次调用axios请求 download url 成功', async () => {
        let input = {} as IGitSourceParams
        input.projectDir = 'any'
        input.branch = 'lastSuccessfulBuild'
        input.host = 'http://8.142.21.209:8080/'
        input.jobPath = '/job/yanbing-demo/'
        input.user = 'yanbing'
        input.password = 'Yun#xia0'
        input.gitRepo = 'yanbing-demo'
        let response = {
            data: {class: '', artifacts: [{relativePath: 'target/app.jar'}]},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/execute-step', {
            axios: axiosStub
        })
        await controller.handleJenkinsSource(input)
        Sinon.assert.calledWithExactly(axiosStub, {
            method: 'get',
            url: `http://8.142.21.209:8080//job/yanbing-demo//lastSuccessfulBuild/api/json`,
            headers: {
                Authorization: `Basic eWFuYmluZzpZdW4jeGlhMA==`
            }
        })
    })
    
    it('第二次调用axios请求 download url 成功', async () => {
        let input = {} as IGitSourceParams
        input.projectDir = 'any'
        input.branch = 'lastSuccessfulBuild'
        input.host = 'http://8.142.21.209:8080/'
        input.jobPath = '/job/yanbing-demo/'
        input.user = 'yanbing'
        input.password = 'Yun#xia0'
        input.gitRepo = 'yanbing-demo'
        let response = {
            data: {class: 'option', artifacts: [{relativePath: 'target/app.jar'}]},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/execute-step', {
            axios: axiosStub
        })
        try{
            await controller.handleJenkinsSource(input)
        }catch(e){
        }
        Sinon.assert.calledWithExactly(axiosStub, {
            method: 'get',
            url: `http://8.142.21.209:8080//job/yanbing-demo//lastSuccessfulBuild/api/json?tree=artifacts%5BrelativePath%5D`,
            headers: {
                Authorization: `Basic eWFuYmluZzpZdW4jeGlhMA==`
            }
        })
    })

    it('第三次调用axios请求 download url 成功', async () => {
        let input = {} as IGitSourceParams
        input.projectDir = 'any'
        input.branch = 'lastSuccessfulBuild'
        input.host = 'http://8.142.21.209:8080/'
        input.jobPath = '/job/yanbing-demo/'
        input.user = 'yanbing'
        input.password = 'Yun#xia0'
        input.gitRepo = 'yanbing-demo'
        let response = {
            data: {class: 'option', artifacts: [{relativePath: 'target/app.jar'}]},
            status: 200
        }
        const axiosStub = Sinon.stub().resolves(response)
        const controller = proxyquire('../src/execute-step', {
            axios: axiosStub
        })
        try{
            await controller.handleJenkinsSource(input)
        }catch(e){}
        Sinon.assert.calledWithExactly(axiosStub, {
            method: 'get',
            url: `http://8.142.21.209:8080//job/yanbing-demo//lastSuccessfulBuild/artifact/target/app.jar`,
            headers: {
                Authorization: `Basic eWFuYmluZzpZdW4jeGlhMA==`
            },
            responseType: 'arraybuffer' as const
        })
    })
})
